﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dblayer;
using Modellayer;


namespace Controllayer
{
    public class ShowCtr
    {
        DbShow dbS;
        DbSeat dbsE;
        DbMovie dbM;

        public ShowCtr()
        {
            dbS = new DbShow();
            dbsE = new DbSeat();
            dbM = new DbMovie();
        }

        public void createShow(DateTime showtime, ShowRoom sh, Movie m)
        {
            Show s = new Show();
            s.movie = m;
            s.showTime = showtime;
            s.showRoom = sh;
            s.showId = dbS.getShowId() + 1;

            int index = 0;
            while (index < s.showRoom.numberOfSeats)
            {
                Seat seat = new Seat();
                seat.show = s;
                seat.seatNumber = index + 1;
                seat.status = 1;
                index++;
            }

            dbS.createShow(s);
            dbsE.createSeats(s);
        }

        public void lockSeat(Show s, int seatNumber)
        {
            Seat seat = findSeat(s, seatNumber);
            if (seat != null)
            {
                seat.status = 2;
                dbsE.UpdateSeat(seat);
            }
        }

        public void unlockSeat(Show s, int seatNumber)
        {
            Seat seat = findSeat(s, seatNumber);
            if (seat != null)
            {
                seat.status = 1;
                dbsE.UpdateSeat(seat);
            }
        }

        public void reserveSeat(Show s, int seatNumber, int reservationsId)
        {
            Seat seat = findSeat(s, seatNumber);
            if (seat != null)
            {
                seat.status = 3;
                seat.reservationId = reservationsId;
                dbsE.UpdateSeat(seat);
            }
        }

        public Show findShow(int showId)
        {
            return dbS.findShow(showId);
        }

        public void createMovie(string title, string movieLength)
        {
            Movie m = new Movie(title, movieLength);
            dbM.createMovie(m);

        }

        private Seat findSeat(Show s, int seatNumber)
        {
            int index = 0;
            Boolean found = false;
            Seat sea = null;
            while (index < s.seats.Count && !found)
            {
                Seat seat = s.seats.ElementAt(index);
                if (sea.seatNumber == seatNumber)
                {
                    found = true;
                    sea = seat;
                }
                else
                {
                    index++;
                }
            }
            return sea;
        }

    }
}
